package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;


    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        // 如果查出来为空,直接返回空
        if (ObjectUtils.isEmpty(apartmentInfo)) {
            return null;
        }
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        // 插入图片列表
        List<GraphVo> graphVoListByApartmentId = getGraphVoListByApartmentId(id);
        apartmentDetailVo.setGraphVoList(graphVoListByApartmentId);
        //插入标签信息列表
        List<LabelInfo> labelInfoList=labelInfoMapper.selectLabelInfoByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //插入配套信息列表
        List<FacilityInfo> facilityInfos=facilityInfoMapper.selectFacilityInfoListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        // 插入最小租金
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId,id).orderByAsc(RoomInfo::getRent).last("limit 1");
        //getOne方法只不过是在2.x的selectOne方法上多了一个布尔值,意思就是如果为true,则就是查询多条记录就会抛异常
        // ,如果为false,则查询多条记录后,取get(0)第一个位置记录返回。
        RoomInfo roomInfo = roomInfoMapper.selectOne(roomQueryWrapper);
        if (ObjectUtils.isNotEmpty(roomInfo)){
            apartmentDetailVo.setMinRent(roomInfo.getRent());
        }
        // 插入公寓信息是否被删除
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted()==1);
        return apartmentDetailVo;
    }

    @Override
    public ApartmentItemVo selectApartmentItemVoByApartmentId(Long apartmentId) {
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        //插入公寓的标签信息列表
        List<LabelInfo> labelInfoList=labelInfoMapper.selectLabelInfoByApartmentId(apartmentId);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        //插入图片信息列表
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId,apartmentId).eq(GraphInfo::getItemType,ItemType.APARTMENT);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphQueryWrapper);
        apartmentItemVo.setGraphVoList(graphInfoList);
        // 插入最小租金
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId,apartmentId).orderByAsc(RoomInfo::getRent).last("limit 1");
        //getOne方法只不过是在2.x的selectOne方法上多了一个布尔值,意思就是如果为true,则就是查询多条记录就会抛异常
        // ,如果为false,则查询多条记录后,取get(0)第一个位置记录返回。
        RoomInfo roomInfo = roomInfoMapper.selectOne(roomQueryWrapper);
        if (ObjectUtils.isNotEmpty(roomInfo)){
            apartmentItemVo.setMinRent(roomInfo.getRent());
        }
        return apartmentItemVo;
    }


    // 插入图片列表
    @Override
    public List<GraphVo> getGraphVoListByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId,apartmentId).eq(GraphInfo::getItemType,ItemType.APARTMENT);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoList.add(graphVo);
        }
        return graphVoList;
    }
}




